package pt.isec.gpsgroup5.moneybox;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class DBAdapter {
	
	private SQLiteDatabase database;
	private DbHelper dbHelper;
	
	//Load Constants from DbHelper class
	private String[] allColumns = { DbHelper.ID, DbHelper.NAME, DbHelper.FINAL_AMOUNT, DbHelper.CURRENT_AMOUNT,
			DbHelper.PRIORITY, DbHelper.YEAR, DbHelper.MONTH, DbHelper.DAY, DbHelper.STATUS};

	//Constructor
	public DBAdapter(Context context) {          
		dbHelper = new DbHelper(context);
		database = dbHelper.getWritableDatabase();
		//database = SQLiteDatabase.openDatabase(DbHelper.DB_PATH + DbHelper.DATABASE_NAME, null,
        //        SQLiteDatabase.OPEN_READWRITE | SQLiteDatabase.CREATE_IF_NECESSARY);
	}
	
	//Creates a new objective in the DB
	public long createObjective(Objective obj) { 
        
		ContentValues values = new ContentValues(); 
        
		values.put(DbHelper.NAME, obj.getName()); 
        values.put(DbHelper.FINAL_AMOUNT, obj.getFinalAmount()); 
        values.put(DbHelper.CURRENT_AMOUNT, obj.getCurrentAmount()); 
        values.put(DbHelper.PRIORITY, obj.getPriority()); 
        values.put(DbHelper.YEAR, obj.getYear()); 
        values.put(DbHelper.MONTH, obj.getMonth()); 
        values.put(DbHelper.DAY, obj.getDay()); 
        values.put(DbHelper.STATUS, obj.getStatus());
        
        return database.insert(DbHelper.TABLE_NAME, null, values);
	}
	
	//Converts a Cursor (pointer) into an Objective
	private Objective cursorToObjective(Cursor cursor) { 

        Objective obj = new Objective(cursor.getInt(0), cursor.getString(1), cursor.getDouble(2), cursor.getDouble(3), cursor.getInt(5), 
        		cursor.getInt(6), cursor.getInt(7), cursor.getInt(4), cursor.getInt(8)); 
        return obj; 
	}
	
	//Deletes the Objective with the id "id"
	public void deleteObjective (int id){ 
        database.delete(DbHelper.TABLE_NAME, DbHelper.ID + " = " + id, null); 
        queueAll();
	}
	
	public Cursor queueAll(){
		 String[] columns = new String[]{DbHelper.ID, DbHelper.NAME, DbHelper.FINAL_AMOUNT, DbHelper.CURRENT_AMOUNT, DbHelper.PRIORITY, DbHelper.YEAR, DbHelper.MONTH, DbHelper.DAY, DbHelper.STATUS};
		 Cursor cursor = database.query(DbHelper.TABLE_NAME, columns,null, null, null, null, null);
		 return cursor;
		}
	
	//Returns an Objective from the DB
	public Objective getObjectives( int id ){ 
		Cursor cursor = database.rawQuery("SELECT * FROM " + DbHelper.TABLE_NAME + " WHERE " + DbHelper.ID + " = " + id, null);

		//If the id is not found returns null
		if ( cursor.getCount() == 1 )
		{
			cursor.moveToFirst(); 
			return cursorToObjective(cursor);
		}
		return null;
	}
	
	public Objective getObjectivePos(int pos){
		Cursor cursor = database.rawQuery("SELECT * FROM " + DbHelper.TABLE_NAME, null);

		//If there is not "pos" objectives return null
		if ( cursor.getCount() > pos )
		{
			cursor.moveToPosition(pos);
			return cursorToObjective(cursor);
		}
		return null;
	}

	
	//Not used
	/*
	public void deactivateObjective (int id){
		ContentValues values = new ContentValues();
		values.put(DbHelper.STATUS, 0);
		
		database.update(DbHelper.TABLE_NAME, values, DbHelper.ID + " = " + id, null);
	}
	*/
	
	//Update an already existing Objective in the DB
	public long updateObjective(Objective obj, int id) { 
        
		ContentValues values = new ContentValues(); 
        
		values.put(DbHelper.NAME, obj.getName()); 
        values.put(DbHelper.FINAL_AMOUNT, obj.getFinalAmount()); 
        values.put(DbHelper.CURRENT_AMOUNT, obj.getCurrentAmount()); 
        values.put(DbHelper.PRIORITY, obj.getPriority()); 
        values.put(DbHelper.YEAR, obj.getYear()); 
        values.put(DbHelper.MONTH, obj.getMonth()); 
        values.put(DbHelper.DAY, obj.getDay()); 
        values.put(DbHelper.STATUS, obj.getStatus());
        
        return database.update(DbHelper.TABLE_NAME, values, DbHelper.ID + " = " + id, null);
	}

	//Opens DB connection
	public SQLiteDatabase open(Context c) {
		dbHelper = new DbHelper(c);
		return dbHelper.getWritableDatabase();
	}
	
	//Check if a name is already in the DB
	public boolean isNameRepeated(String name){
		String[]sName = {name};
		Cursor c = database.query(DbHelper.TABLE_NAME, null, DbHelper.NAME + " = ?", sName, null, null, null);
		if (c.getCount() == 0){
			c.close();
			return false;
		}
		c.close();
		return true;
	}
	
	//Returns next available id
	public int getNextAvailableID(){
		int id;
		Cursor cCount = database.query(DbHelper.TABLE_NAME, null, null, null, null, null, null);
		if(cCount.getCount() > 0) {
			cCount.moveToLast();
			id = cCount.getInt(0) + 1;
			cCount.close();
			return id;
		}
		cCount.close();
		return 1;
	}
	
	/*public List<HashMap<String, String>> getObjectivesList(){
		
	}*/
	
	public int numObjectives(){
		Cursor cCount = database.query(DbHelper.TABLE_NAME, null, null, null, null, null, null);
		int num = cCount.getCount();
		cCount.close();
		return num;
	}
	
	public void close(){
		database.close();
	}
	
	
}
